package ar.edu.unlu.sistemas.p2p.business.msg;

import java.net.InetSocketAddress;

import org.jdom.Element;

import ar.edu.unlu.sistemas.p2p.business.vo.PingResultVO;

public class PingResult {
	
	public static final String TAG_ROOT = "result";
	private static final String TAG_ADDR = "address";
	private static final String TAG_FILES = "nfiles";
	
	private String address;
	private int nFiles;
	
	public PingResult() {
		
	}
	
	public PingResult(String address, int nFiles) {
		this.address = address;
		this.nFiles = nFiles;
	}
	
	public PingResult(InetSocketAddress address, int nFiles) {
		this.address = address.getAddress().getHostAddress().toString()+":"+String.valueOf(address.getPort());
		this.nFiles = nFiles;
	}
	
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public int getnFiles() {
		return nFiles;
	}
	public void setnFiles(int nFiles) {
		this.nFiles = nFiles;
	}
	
	public Element toXMLElement() {
		Element el = new Element(TAG_ROOT);
		el.addContent(new Element(TAG_ADDR).setText(this.getAddress()));
		el.addContent(new Element(TAG_FILES).setText(String.valueOf(this.getnFiles())));
		return el;
	}
	
	public static PingResult buildFromXMLElement(Element headerElement) {
		PingResult result =new PingResult();
		result.setAddress(headerElement.getChild(TAG_ADDR).getText());
		result.setnFiles(Integer.parseInt(headerElement.getChild(TAG_FILES).getText()));
		return result;
	}
	
	public PingResultVO getVO() {
		return new PingResultVO(this.getAddress(), this.getnFiles());
	}

	

}
